package com.chencs.common.user.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.common.user.service.UserService;
import com.chencs.vo.common.user.UserModel;
import framework.model.ResponseOptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by chencs on 2017/6/7.
 */
@RequestMapping("/login")
@RestController
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    UserService userService;

    @RequestMapping("/loginIndex")
    public ModelAndView loginIndex() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody UserModel model) {
        logger.info("/login/login param: {}", JSON.toJSON(model) );
        String retStr = "";
        try {
            retStr = userService.login(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView home() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/home");
        return modelAndView;
    }

    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public ModelAndView main() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/index");
        return modelAndView;
    }



    @RequestMapping(value = "/help", method = RequestMethod.GET)
    public ModelAndView help() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("wd/index");
        return modelAndView;
    }

    @RequestMapping(value = "/website", method = RequestMethod.GET)
    public ModelAndView website() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("wd/app/index");
        return modelAndView;
    }
}
